package com.link.base.base.accnt.controller;

import com.link.base.base.accnt.model.AccountEmail;
import com.link.base.base.accnt.service.AccountEmailService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户邮件controller
 *
 * @author unknown
 * @version 1.0
 * @date: 2018/11/7 9:54
 */

@Controller
@RequestMapping("/link/accountEmail")
public class AccountEmailController extends BasicController<AccountEmail> {

    @Resource
    private AccountEmailService accountEmailService;

    @Override
    public BasicService<AccountEmail> getBasicService() throws Exception {
        return accountEmailService;
    }

    /**
     * 查询邮件列表
     *
     * @param qps
     * @param session
     * @param request
     * @param response
     * @return
     */

    @RequestMapping(value = "/queryByExamplePage", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryByExamplePage(@JsonParam QueryParams qps, HttpSession session,
                                                  HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        List<AccountEmail> list = null;
        AccountEmail entity = new AccountEmail();
        try {
            qps.invokePreFilterAndSorter();
            qps.preDealSecurity(request);
            AccountEmail t = (AccountEmail) BasicModel.transformClass(entity.getClass(), qps);
            beforQueryAllOrExample(t, request);
            beforQueryExample(t, request);
            if (qps.getAttr1() != null && !"".equals(qps.getAttr1())) {
                t.setAcctId(Long.parseLong(qps.getAttr1()));
            } else {
                throw new ServiceException("PUBLIC-026!");
            }
            list = accountEmailService.queryByExamplePage(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount(t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<AccountEmail>());
            result.put("result", e.getMessage());
        }
        return result;
    }

}
